/**
 * Title AppCategoryController
 * Package com.sba.manage.controller
 * Copyright 2024 www.hundsun.com All Rights Reserved.
 *
 * @author gd
 * @date 2024/9/9 1:57 AM
 * @version V5.1.2
 */
package com.ruoyi.manage.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.app.core.RespEntity;
import com.ruoyi.common.utils.TokenUtils;
import com.ruoyi.manage.db.model.SbaAudioFiles;
import com.ruoyi.manage.db.model.SbaAudioFilesSmp;
import com.ruoyi.manage.db.model.SbaLevelCategories;
import com.ruoyi.manage.service.IAudioFiles;
import com.ruoyi.manage.service.ILevelCategories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 首页类别
 * ClassName AppCategoryController
 *
 * @author gd
 * @date 2024/9/9 1:57 AM
 */
@Controller
@RequestMapping("/app/home")
public class AppCategoryController {

    @Autowired
    private ILevelCategories levelCategoriesServiceImpl;

    @Autowired
    private IAudioFiles audioFilesServiceImpl;

    @PostMapping("/list")
    @ResponseBody
    public RespEntity homelist(@Validated String token,
                           @Validated String userID) {
        if (TokenUtils.validateToken(token, userID)) {
            return RespEntity.fail("token已失效，请重新登录！");
        }
        List<SbaLevelCategories> ls = levelCategoriesServiceImpl.queryCategoriesList();
        return RespEntity.ok(ls);
    }

    /**
     *  音频列表
     * @param token token
     * @param userID 用户 id
     * @param categoryId 类别
     * @param model 模式
     * @param pageNum 第几页
     * @param pageSize 本页大小
     * @return 响应信息
     */
    @PostMapping("/audiolist")
    @ResponseBody
    public RespEntity audiolist(@Validated String token,
                                   @Validated String userID,
                                   @Validated String categoryId,
                                   @Validated String model,
                                   @Validated int pageNum,
                                   @Validated int pageSize) {
        if (TokenUtils.validateToken(token, userID)) {
            return RespEntity.fail("token已失效，请重新登录！");
        }
        PageInfo<SbaAudioFilesSmp> ls = audioFilesServiceImpl.queryListByCategoryId(categoryId, pageNum, pageSize);
        return RespEntity.ok(ls);
    }

    /**
     * 获取单个音频信息
     * @param token token
     * @param userID 用户 id
     * @param audioFileId 音频 id
     * @return 响应信息
     */
    @PostMapping("/audio")
    @ResponseBody
    public RespEntity audiolist(@Validated String token,
                                @Validated String userID,
                                @Validated String audioFileId) {
        if (TokenUtils.validateToken(token, userID)) {
            return RespEntity.fail("token已失效，请重新登录！");
        }
        SbaAudioFiles sbf = audioFilesServiceImpl.queryListByAudioFileId(audioFileId);
        return RespEntity.ok(sbf);
    }

    /**
     * 收藏文件
     * @param token token
     * @param userID 用户 id
     * @param audioFileId 音频 id
     * @return 响应结果
     */
    @PostMapping("/favorite")
    @ResponseBody
    public RespEntity favoritefile(@Validated String token,
                                @Validated String userID,
                                @Validated String audioFileId) {
        if (TokenUtils.validateToken(token, userID)) {
            return RespEntity.fail("token已失效，请重新登录！");
        }
        audioFilesServiceImpl.favoritefile(audioFileId,userID);
        return RespEntity.ok();
    }
}